package com.example.demo.order.service.impl;

import com.example.demo.order.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ProductServiceImpl
 * @Description 商品服务实现类（模拟实现）
 * @Author MingChang.Wei
 * @Date 2025/9/28
 **/
@Slf4j
@Service
public class ProductServiceImpl implements ProductService {
    
    // 模拟商品数据
    private static final Map<Long, ProductInfo> PRODUCT_DATA = new HashMap<>();
    
    static {
        // 初始化一些测试商品数据
        PRODUCT_DATA.put(1L, new ProductInfo("华为手机 Mate60", new BigDecimal("5999.00"), "https://via.placeholder.com/128?text=Phone", "白色-128GB"));
        PRODUCT_DATA.put(2L, new ProductInfo("苹果笔记本 MacBook Air", new BigDecimal("8999.00"), "https://via.placeholder.com/128?text=Laptop", "银色-256GB"));
        PRODUCT_DATA.put(3L, new ProductInfo("小米智能手表", new BigDecimal("699.00"), "https://via.placeholder.com/128?text=Watch", "黑色-标准版"));
        PRODUCT_DATA.put(4L, new ProductInfo("戴尔显示器 27寸", new BigDecimal("1299.00"), "https://via.placeholder.com/128?text=Monitor", "黑色-4K"));
        PRODUCT_DATA.put(5L, new ProductInfo("罗技无线鼠标", new BigDecimal("199.00"), "https://via.placeholder.com/128?text=Mouse", "白色-无线版"));
    }
    
    @Override
    public String getProductName(Long productId) {
        ProductInfo product = PRODUCT_DATA.get(productId);
        if (product != null) {
            return product.getName();
        }
        log.warn("商品不存在, productId={}", productId);
        return "未知商品" + productId;
    }
    
    @Override
    public BigDecimal getProductPrice(Long productId) {
        ProductInfo product = PRODUCT_DATA.get(productId);
        if (product != null) {
            return product.getPrice();
        }
        log.warn("商品不存在, productId={}", productId);
        return new BigDecimal("99.00"); // 默认价格
    }
    
    @Override
    public String getProductImageUrl(Long productId) {
        ProductInfo product = PRODUCT_DATA.get(productId);
        if (product != null) {
            return product.getImageUrl();
        }
        log.warn("商品不存在, productId={}", productId);
        return "https://via.placeholder.com/128?text=Unknown";
    }
    
    @Override
    public String getProductSku(Long productId) {
        ProductInfo product = PRODUCT_DATA.get(productId);
        if (product != null) {
            return product.getSku();
        }
        log.warn("商品不存在, productId={}", productId);
        return "默认规格";
    }
    
    @Override
    public boolean isProductExists(Long productId) {
        return PRODUCT_DATA.containsKey(productId);
    }
    
    /**
     * 商品信息内部类
     */
    private static class ProductInfo {
        private final String name;
        private final BigDecimal price;
        private final String imageUrl;
        private final String sku;
        
        public ProductInfo(String name, BigDecimal price, String imageUrl, String sku) {
            this.name = name;
            this.price = price;
            this.imageUrl = imageUrl;
            this.sku = sku;
        }
        
        public String getName() {
            return name;
        }
        
        public BigDecimal getPrice() {
            return price;
        }
        
        public String getImageUrl() {
            return imageUrl;
        }
        
        public String getSku() {
            return sku;
        }
    }
}
